package com.txx.app.main.commom.mvvm;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qihoo360.replugin.RePlugin;
import com.trello.rxlifecycle.components.support.RxFragment;
import com.txx.app.main.R;
import com.txx.app.main.commom.ui.MyDialog;
import com.txx.app.main.commom.ui.MyToast;


/**
 * Created by renshiqian on 2018/10/30.
 */

public class MVVMBaseFragment extends RxFragment {
    private Dialog dialog;
    public Activity activity;
    private MyDialog meDialog;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach (activity);
        this.activity=activity;
    }


    @Override
    public Context getContext() {
        return activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        createProgressDialog();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    /**
     * 显示progressDialog
     */
    public boolean showProgressDialog() {
        if (dialog != null && !activity.isFinishing()) {
            dialog.show();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 关闭progressDialog
     */
    public void dismissProgressDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 关闭dialog后，可以使用
     */
    public void nullProgressDialog() {
        dialog = null;
    }

    public void createProgressDialog() {
        if(dialog==null){
            View layout = View.inflate(activity, R.layout.dialog_loading_bg, null);
            dialog = new Dialog(activity, R.style.dialog);
            dialog.setCancelable(true);
            dialog.setContentView(layout);
        }

    }

    private void createProgressDialog2() {
        View layout = View.inflate(activity, R.layout.dialog_loading_bg2, null);
        dialog = new Dialog(activity, R.style.dialog);
        dialog.setCancelable(true);
        dialog.setContentView(layout);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        dismissProgressDialog();

    }
    public void showError(String msg) {
        MyToast.showShort(msg);
    }

    public void showErrorDialog(String msg) {
        if (meDialog == null) {
            meDialog = new MyDialog(getActivity());
            return;
        }
        meDialog.showDialog(R.layout.dialog_verify);
        TextView tv_msg = (TextView) meDialog.findViewById(R.id.tv_warn_msg);
        tv_msg.setText(msg);
        meDialog.findViewById(R.id.tv_warn_true).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                meDialog.dismiss();
            }
        });
    }

    public  boolean isBackPressed(){
        return true;
    }
}
